package com.kuangstudy.io.buffer;

import java.io.*;


/**
 * ClassName: UnsafeBuyTicket
 * date: 2021/07/16 16:25:53
 *
 * @author zlk
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        show5();
    }

    /**
     * BufferedReader
     * 特有成员方法
     * readLine() 读取一行文本 返回null
     */
    private static void show5() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("e:\\2.txt"));
        String line ;
        while ((line = br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }

    /**
     * BufferedWriter
     * 特有成员方法
     * void newLine() 写入一个行分隔符。会根据不同的操作系统，获取不同的行分隔符
     */
    private static void show4() throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("e:\\2.txt"));
        for (int i = 0; i < 10; i++) {
            bw.write("坤坤好帅"+i);
//            bw.write("\n");
            bw.newLine();
        }
        bw.close();
    }

    private static void show3() throws IOException {
        long s = System.currentTimeMillis();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("e:\\1.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\1.jpg"));
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();
        bis.close();
        long e = System.currentTimeMillis();
        System.out.println("耗时" + (e - s) + "毫秒");//8

    }

    /**
     * BufferedInputStream会给FileInputStream增加一个缓冲区，提高FileInputStream的读取效率
     */
    private static void show2() throws IOException {
        FileInputStream fis = new FileInputStream("e:\\2.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, len));
        }
        bis.close();//关闭缓冲流 自动关闭FileInputStream
    }

    /**
     * BufferedOutputSteam(OutputSteam out)创建一个新的缓冲输出流 将数据写入指定的底层输出流
     * 提高效率
     * BufferedOutputSteam(OutputSteam out,int size)创建一个新的缓冲输出流并指定缓冲区大小
     */
    private static void show1() throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\\2.txt"));
        bos.write("我把数据写入到缓冲区中".getBytes());
        bos.close();

    }
}
